﻿using System;
using BLL;
using BLL.Access;
using Entity;
using Web.Class;

namespace Web.Pages.QuShui.DiXiaS.ShouFeiGL
{
    public partial class KaiZhang : BasePage
    {
        #region 属性

        #endregion

        #region 变量

        /// <summary>
        ///     数据库
        /// </summary>
        private readonly SpHelper _spHelper = new SpHelper();

        /// <summary>
        ///     用户信息bll
        /// </summary>
        private QS_YG_YONGHUXXBLL _YHXXBLL = new QS_YG_YONGHUXXBLL();

        #endregion

        #region 事件

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) InitControl();
        }

        /// <summary>
        ///     查错按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnSelectError_Click(object sender, EventArgs e)
        {
            LabNum.Visible = false;
            BtnKZ.ClientEnabled = false;
            SearchError();
        }

        /// <summary>
        ///     /汇总按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnKZ_Click(object sender, EventArgs e)
        {
            LabNum.Visible = false;
            SaveKaiZhang();
        }

        //protected void ASPxCallback1_Callback(object source, DevExpress.Web.CallbackEventArgs e)
        //{
        //    if (e.Parameter == "checkerror")
        //    {

        //        int code = -1;
        //        string msg = "";

        //        string newids = "";
        //        if (HiddenList.Contains("glkcheck"))
        //        {
        //            string glk = HiddenList.Get("glkcheck").ToString().Split(',')[0];
        //            string[] ids = HiddenList.Get("glkcheck").ToString().Split(',');

        //            if (glk.Trim().Length > 0)
        //            {
        //                foreach (string s in ids)
        //                {
        //                    if (s.Trim() != glk.Trim())
        //                    {
        //                        newids += s + ",";
        //                    }
        //                }
        //               // msg = CheckError(glk);
        //                code = 1;
        //            }
        //            else
        //            {
        //                code = 2;
        //            }
        //        }
        //        e.Result = e.Parameter + "|" + code + "|" + msg + "|" + newids;
        //    }
        //    if (e.Parameter == "sumdata")
        //    {
        //        int code = -1;
        //        string msg = "";

        //        string newids = "";
        //        if (HiddenList.Contains("glk"))
        //        {
        //            string glk = HiddenList.Get("glk").ToString().Split(',')[0];
        //            string[] ids = HiddenList.Get("glk").ToString().Split(',');

        //            if (glk.Trim().Length > 0)
        //            {
        //                foreach (string s in ids)
        //                {
        //                    if (s.Trim() != glk.Trim())
        //                    {
        //                        newids += s + ",";
        //                    }
        //                }
        //                msg = "";// ToKaiZhang(glk);
        //                code = 1;
        //            }
        //            else
        //            {
        //                code = 2;
        //            }
        //        }
        //        e.Result = e.Parameter + "|" + code + "|" + msg + "|" + newids;
        //    }
        //}

        #endregion

        #region 方法

        /// <summary>
        ///     页面首次加载绑定控件
        /// </summary>
        private void InitControl()
        {
            var wtype = 2;
            txtShuiLiangYM.Text = BusinessTool.GetSLYMorZWNY(1, wtype).ToString();
            txtZhuanHuanJG.Visible = true;
        }

        /// <summary>
        ///     查错
        /// </summary>
        private void SearchError()
        {
            txtZhuanHuanJG.Text = "";
            txtZhuanHuanJG.Visible = true;
            var yd = txtShuiLiangYM.Text;

            var syear = 0;
            var smonth = 0;

            if (yd != null && yd.Trim().Length == 6)
            {
                syear = Convert.ToInt32(yd.Substring(0, 4));
                smonth = Convert.ToInt32(yd.Substring(4, 2));
            }
            else
            {
                PageTools.AlertMsg(this, "水量年月输入不正确！");
                txtShuiLiangYM.Focus();
                return;
            }

            var allmsg = "";
            var userid = LoginInfo.CurrentUser.UserID;

            //验证1
            var dt1 = SearchSpHelper.P_QSSF_KZ_CHECKERROR(2, syear, smonth, 0).Tables[0];
            if (dt1 != null && dt1.Rows.Count > 0)
            {
                PageTools.AlertMsg(this, "抄表数据没有完成，请检查！");
                for (var i = 0; i < dt1.Rows.Count; i++) allmsg = allmsg + "：水表" + dt1.Rows[i][1] + "未录入" + "\r\n";
                txtZhuanHuanJG.Text = allmsg;
                return;
            }

            //验证2
            var dt3 = _spHelper.P_QSSF_KZ_CHECKERROR2(2, syear, smonth, 1).Tables[0];
            if (dt3 != null && dt3.Rows.Count > 0)
            {
                PageTools.AlertMsg(this, "抄表数据里有重复的抄表记录，请检查！");
                return;
            }

            var CSBLL = new QS_CANSHUBBLL();
            var dt4 = CSBLL.SelectQS_CANSHUBsDynamic("S_MINGCHENG=\'AccountMonth\' AND I_JLZT=0", null);
            var zwm = dt4.Rows.Count > 0 ? Convert.ToInt32(dt4.Rows[0]["S_ZHI"]) : 0;
            var zwm2 = DateTime.Now.Month;
            if (zwm < zwm2)
            {
                PageTools.AlertMsg(this, "当前账务年月为" + zwm + "，请确认！");
                return;
            }
            /* comment by gdj at 17-03-31
            //验证3
            DataTable dt2 = _YHXXBLL.SelectQS_YG_YONGHUXXesDynamic(string.Format("I_JLZT=0 AND I_WTYPE={0} AND S_YONGHUH NOT IN (SELECT S_YONGHUH FROM QS_YH_YONGHUSBGX WHERE I_JLZT=0)",BusinessTool.WType), null);
                  
            if (dt2 != null && dt2.Rows.Count > 0)
            {
                PageTools.AlertMsg(this, "存在有户无表的用户，请检查！");
                for (int i = 0; i < dt2.Rows.Count; i++)
                {
                    allmsg = allmsg  + "用户" + dt2.Rows[i]["S_YONGHUH"].ToString() + "无表" + "\r\n";
                }
                txtZhuanHuanJG.Text = allmsg;
                    
            }
             * */

            allmsg = "查错完成,可以开账";
            BtnKZ.ClientEnabled = true;
            txtZhuanHuanJG.Text = allmsg;
        }


        /// <summary>
        ///     开账操作
        /// </summary>
        private void SaveKaiZhang()
        {
            var wtype = 2;
            var userid = LoginInfo.CurrentUser.UserID;
            txtZhuanHuanJG.Visible = true;
            var yd = txtShuiLiangYM.Text;
            var zwym = BusinessTool.GetSLYMorZWNY(2, wtype);
            var syear = 0;
            var smonth = 0;
            if (yd != null && yd.Trim().Length == 6)
            {
                syear = Convert.ToInt32(yd.Substring(0, 4));
                smonth = Convert.ToInt32(yd.Substring(4, 2));
            }

            var sfyx = chkKaiZhang.Checked ? "1" : "0";
            //string Msg = "";
            //string ErrMsg = "";
            var allmsg = "";
            var con = 0;

            ////开采井和矿泉井，专灌和代灌
            //DataTable dt1 = SearchSpHelper.P_QSSF_KZ_CHECKERROR(BusinessTool.WType, syear, smonth, 1).Tables[0];
            //if (dt1 != null && dt1.Rows.Count > 0)
            //{
            //    PageTools.AlertMsg(this, "本月开出去的帐已有到帐的数据,不能重新开帐");
            //    return;
            //}
            //开账,开账完成后，直接托收，合并到一个存储过程中
            var dt = SearchSpHelper.P_QSSF_KAIZHANG(wtype, sfyx, syear, smonth, userid.ToString(), zwym, 0).Tables[0];
            //开账完成后，执行托收
            // DataTable dt_ts = _spHelper.P_QSSF_BENYUETS(wtype, syear, smonth, userid, "").Tables[0];
            // && dt_ts.Rows[0][0].ToString() == "1"
            if (dt != null && dt.Rows.Count > 0)
                for (var i = 0; i < dt.Rows.Count; i++)
                    allmsg = allmsg + dt.Rows[i]["S_SHUIBIAOH"] + "：开账完成\r\n";
            con = dt.Rows.Count > 0 ? dt.Rows.Count : 0;


            txtZhuanHuanJG.Text = allmsg + "共" + con + "条";
        }

        protected void BtnJJKZ_Click(object sender, EventArgs e)
        {
        }

        //protected void ASPxCallback1_Callback(object source, DevExpress.Web.CallbackEventArgs e)
        //{

        //    int wtype = 2;// BusinessTool.WType;
        //    int y = Convert.ToInt32(txtShuiLiangYM.Text) / 100;
        //    int m = Convert.ToInt32(txtShuiLiangYM.Text) % 100;
        //    QS_CANSHUBBLL CSBLL = new QS_CANSHUBBLL();


        //    e.Result = e.Parameter + "|0|" + msg;
        //}

        #endregion
    }
}